package com.laker.admin.module.inv.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laker.admin.framework.model.PageResponse;
import com.laker.admin.framework.model.Response;
import com.laker.admin.module.inv.entity.InvReport;
import com.laker.admin.module.inv.service.IInvReportService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author laker
 * @since 2024-03-21
 */
@RestController
@RequestMapping("/inv/report")
public class InvReportController {
    @Autowired
    IInvReportService invReportService;

    @GetMapping
    @ApiOperation(value = "分页查询")
    public PageResponse pageAll(@RequestParam(required = false, defaultValue = "1") long page,
                                @RequestParam(required = false, defaultValue = "10") long limit,
                                String keyWord) {
        Page roadPage = new Page<>(page, limit);
        LambdaQueryWrapper<InvReport> queryWrapper = new QueryWrapper().lambda();

        queryWrapper.and(StrUtil.isNotBlank(keyWord), i -> i.
                like(StrUtil.isNotBlank(keyWord), InvReport::getCommName, keyWord)
                .or().like(StrUtil.isNotBlank(keyWord), InvReport::getCommName, keyWord));

        Page pageList = invReportService.page(roadPage, queryWrapper);
        return PageResponse.ok(pageList.getRecords(), pageList.getTotal());
    }

    @GetMapping("/getAll")
    @ApiOperation(value = "查询所有")
    public Response getAll(String keyWord) {
        LambdaQueryWrapper<InvReport> queryWrapper = new QueryWrapper().lambda();
        queryWrapper.and(StrUtil.isNotBlank(keyWord), i -> i.
                like(StrUtil.isNotBlank(keyWord), InvReport::getCommName, keyWord)
                .or().like(StrUtil.isNotBlank(keyWord), InvReport::getCommName, keyWord));
        return Response.ok(invReportService.getAll(queryWrapper));
    }
}
